﻿using System;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Text;

using BLL;
using Microsoft.Reporting.WebForms;
using Model;

namespace EvaluationSystem.Report
{
    public partial class RptViewer : System.Web.UI.Page
    {
        //hide unwanted export format.
        private void ReflectivelySetVisibilityFalse(RenderingExtension extension)
        {
            FieldInfo info = extension.GetType().GetField("m_isVisible",
            BindingFlags.NonPublic | BindingFlags.Instance);
            if (info != null)
            {
                info.SetValue(extension, false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["sn"] != null)
                {
                    string strGUID = Request.QueryString["sn"].ToString();
                    if (!string.IsNullOrEmpty(strGUID))
                    {
                        foreach (RenderingExtension extension in ReportViewer1.LocalReport.ListRenderingExtensions())
                        {
                            if (extension.Name != "PDF" && extension.Name != "WORD")
                            {
                                ReflectivelySetVisibilityFalse(extension);
                            }
                        }

                        RptBLL rptBLL = new RptBLL();
                        ActivityBLL aBLL = new ActivityBLL();
                        GuidBLL guidBLL = new GuidBLL();
                        CommentBLL cBLL = new CommentBLL();

                        DataTable dtTmp = rptBLL.GetDimensionScore(strGUID);
                        dtTmp.DefaultView.Sort = "DIMCLASSID ASC";
                        DataTable dimTable = dtTmp.DefaultView.ToTable();

                        DataTable testerInfoTable = rptBLL.GetTesterInfo(strGUID);
                        DimensionBLL dimbll = new DimensionBLL();

                        double totalMinutes = rptBLL.GetRefTotalTimeMinute(strGUID);
                        double refMinMinutes = 0;
                        double refMaxMinutes = 0;

                        if (totalMinutes > 12.1)
                        {
                            refMinMinutes = totalMinutes - (double)12.1;
                        }
                        refMaxMinutes = totalMinutes + 12.1;

                        string strREFtime = refMinMinutes.ToString("F1") + " ～ " + refMaxMinutes.ToString("F1");

                        string shcxxDENGJI = string.Empty;
                        string shcxxPINGDING = string.Empty;
                        string shcxxMIAOSHU = string.Empty;

                        string cxbxPY = string.Empty;//诚信表现评语

                        int aID = guidBLL.GetActivityId(strGUID);
                        int positionId = aBLL.GetPositionId(aID);
                        Activity a = aBLL.GetModel(aID);
                        int rptSettings = a.Activity_viewreport;

                        bool p5Visible = true;
                        bool p6Visible = true;

                        if (rptSettings != 0 && rptSettings.ToString().Length > 1)
                        {
                            p5Visible = rptSettings.ToString().Substring(5, 1).Equals("1");
                            p6Visible = rptSettings.ToString().Substring(6, 1).Equals("1");
                        }

                        ArrayList arGZZT_youshi = new ArrayList();//工作状况_优势
                        ArrayList arGZZT_lieshi = new ArrayList();//工作状况_劣势

                        string youshi = string.Empty;
                        string lieshi = string.Empty;

                        StringBuilder sbGZZT = new StringBuilder();//工作状况
                        StringBuilder sbTZMS = new StringBuilder();//特质描述
                        StringBuilder sbYRJY = new StringBuilder();//用人建议
                        StringBuilder sbGRFZ = new StringBuilder();//个人发展

                        //作答结果是否可信>>>>>>>
                        bool b1 = false;//实际作答时间是否落在参考值区间
                        bool b2 = false;//社会称许性等级得分大于7.9
                        bool b3 = false;//作答一致性处于"较高"的等级
                        //作答结果是否可信<<<<<<<

                        for (int i = 0; i < dimTable.Rows.Count; i++)
                        {
                            int dimID = Int32.Parse(dimTable.Rows[i][0].ToString());//维度ID
                            int dimscore = Int32.Parse(dimTable.Rows[i][2].ToString());//维度原始分
                            Double dim_s = Double.Parse(dimTable.Rows[i][3].ToString());//维度参考区间开始分
                            Double dim_e = Double.Parse(dimTable.Rows[i][4].ToString());//维度参考区间结束分

                            //Double dimQ1 = Double.Parse(dimTable.Rows[i][9].ToString());//Q1
                            //Double dimQ2 = Double.Parse(dimTable.Rows[i][10].ToString());//Q2
                            //Double dimQ3 = Double.Parse(dimTable.Rows[i][11].ToString());//Q3


                            Double p17 = Double.Parse(dimTable.Rows[i][9].ToString());
                            Double p68 = Double.Parse(dimTable.Rows[i][10].ToString());
                            Double p92 = Double.Parse(dimTable.Rows[i][11].ToString());

                            if (dimscore <= p17)
                            {
                                dimTable.Rows[i][8] = "偏低";
                            }
                            else if (dimscore > p17 && dimscore <= p68)
                            {
                                dimTable.Rows[i][8] = "稍低";
                            }
                            else if (dimscore > p68 && dimscore < p92)
                            {
                                dimTable.Rows[i][8] = "适配";
                            }
                            else if (dimscore >= p92)
                            {
                                dimTable.Rows[i][8] = "偏高";
                            }

                            //维度内判断诚信等级>>>>>>>>>>>>>>>>>>>>>>>>>>>>
                            if (dimTable.Rows[i][1].ToString().Trim().Equals("诚信"))
                            {
                                if (dimscore <= 58.7)
                                {
                                    cxbxPY = "有待提高";
                                }
                                else if (dimscore >= 67.1)
                                {
                                    cxbxPY = "比较诚信";
                                }
                                else if (dimscore > 58.7 && dimscore < 67.1)
                                {
                                    cxbxPY = "中等";
                                }
                            }
                            //维度内判断诚信等级<<<<<<<<<<<<<<<<<<<<<<<<<<<<


                            //获取评语>>>>>>>>>>>>>>>>>>>>>>>>>>>>
                            for (int kk = 1; kk < 5; kk++)
                            {
                                if (dimTable.Rows[i][8].ToString().Trim().Equals("偏低"))
                                {
                                    string c = cBLL.GetCommentByAll(positionId, dimID, kk, 4);
                                    if (kk == 1)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbTZMS.Append((i + 1).ToString() + "、" + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 2)
                                    {                                        
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbGZZT.Append((i + 1).ToString() + "、" + c + Environment.NewLine+Environment.NewLine);
                                            arGZZT_lieshi.Add(c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 3)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbYRJY.Append("　　* " + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 4)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbGRFZ.Append("　　* " + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }

                                    dimTable.Rows[i][12] = cBLL.GetCommentByAll(positionId, dimID, 1, 4);
                                }
                                else if (dimTable.Rows[i][8].ToString().Trim().Equals("稍低"))
                                {
                                    string c = cBLL.GetCommentByAll(positionId, dimID, kk, 3);
                                    if (kk == 1)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbTZMS.Append((i + 1).ToString() + "、" + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 2)
                                    {                                        
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbGZZT.Append((i + 1).ToString() + "、" + c + Environment.NewLine+Environment.NewLine);
                                            arGZZT_lieshi.Add(c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 3)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbYRJY.Append("　　* " + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 4)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbGRFZ.Append("　　* " + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    dimTable.Rows[i][12] = cBLL.GetCommentByAll(positionId, dimID, 1, 3);
                                }
                                else if (dimTable.Rows[i][8].ToString().Trim().Equals("适配"))
                                {
                                    string c = cBLL.GetCommentByAll(positionId, dimID, kk, 2);
                                    if (kk == 1)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbTZMS.Append((i + 1).ToString() + "、" + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 2)
                                    {                                        
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbGZZT.Append((i + 1).ToString() + "、" + c + Environment.NewLine+Environment.NewLine);
                                            arGZZT_youshi.Add(c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 3)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbYRJY.Append("　　* " + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 4)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbGRFZ.Append("　　* " + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }

                                    dimTable.Rows[i][12] = cBLL.GetCommentByAll(positionId, dimID, 1, 2);
                                }
                                else if (dimTable.Rows[i][8].ToString().Trim().Equals("偏高"))
                                {
                                    string c = cBLL.GetCommentByAll(positionId, dimID, kk, 1);
                                    if (kk == 1)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbTZMS.Append((i + 1).ToString() + "、" + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 2)
                                    {                                        
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbGZZT.Append((i + 1).ToString() + "、" + c + Environment.NewLine+Environment.NewLine);
                                            arGZZT_youshi.Add(c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 3)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbYRJY.Append("　　* " + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    else if (kk == 4)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            sbGRFZ.Append("　　* " + c + Environment.NewLine+Environment.NewLine);
                                        }
                                    }
                                    dimTable.Rows[i][12] = cBLL.GetCommentByAll(positionId, dimID, 1, 1);
                                }
                            }
                            //获取评语<<<<<<<<<<<<<<<<<<<<<<<<<<<<    

                          

                            //维度内判断社会称许性等级>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
                            if (dimTable.Rows[i][1].ToString().Trim().Equals("社会称许性"))
                            {
                                double score = double.Parse(dimTable.Rows[i][2].ToString());
                                if (score >= 0 && score <= 2.1)
                                {
                                    shcxxDENGJI = "较高";
                                    shcxxPINGDING = "倾向偏强";
                                    shcxxMIAOSHU = "他（她）在本次答题时表现出较强的获取赞许的倾向，这种倾向可能是有意识的，也可能是无意识的，不能排除潜意识的影响";
                                }
                                else if (score >= 2.2 && score <= 4.0)
                                {
                                    shcxxDENGJI = "中等";
                                    shcxxPINGDING = "可接受";
                                    shcxxMIAOSHU = "他（她）在本次答题时表现出与大多数人相似的称许性倾向";
                                }
                                else if (score >= 4.1 && score <= 16.0)
                                {
                                    shcxxDENGJI = "较低";
                                    shcxxPINGDING = "可接受";
                                    shcxxMIAOSHU = "他（她）在本次答题中表现出较低的社会称许性倾向，通俗说，比起大多数人，他（她）有点不太在乎别人的评价，能够较客观的进行答题";
                                    if (score > 7.9)
                                    {
                                        b2 = true;
                                    }
                                }                               
                            }

                            //维度内判断社会称许性等级<<<<<<<<<<<<<<<<<<<<<<<<<<<<

                            //原始分转化为标准分>>>>>>>>>>>>>>>>>>>>>>
                            if (dimTable.Rows[i][5].ToString().Trim().Equals("3"))//智力潜能类
                            {
                                dimTable.Rows[i][2] = rptBLL.ConvertZhiLiDimScore(dimID, dimscore);
                                dimTable.Rows[i][3] = rptBLL.ConvertZhiLiDimScore(dimID, dim_s);
                                dimTable.Rows[i][4] = rptBLL.ConvertZhiLiDimScore(dimID, dim_e);
                            }
                            else
                            {
                                dimTable.Rows[i][2] = rptBLL.ConvertDimScore(dimID, dimscore);
                                dimTable.Rows[i][3] = rptBLL.ConvertDimScore(dimID, dim_s);
                                dimTable.Rows[i][4] = rptBLL.ConvertDimScore(dimID, dim_e);
                            }
                            //原始分转化为标准分<<<<<<<<<<<<<<<<<<<<<<<<<<
                        }

                        //工作劣势
                        for (int mm = 0; mm < arGZZT_lieshi.Count; mm++)
                        {
                            lieshi += "　　" + (mm + 1).ToString() + "、" + arGZZT_lieshi[mm].ToString();
                        }

                        //工作优势
                        for (int nn = 0; nn < arGZZT_youshi.Count; nn++)
                        {
                            youshi += "　　" + (nn + 1).ToString() + "、" + arGZZT_youshi[nn].ToString();
                        }

                        //作答一致性等级，判定>>>>>>>>>>>>>>>>>>
                        string trustful = string.Empty;
                        string trustfulComments = string.Empty;
                        int nTrustful = Int32.Parse(testerInfoTable.Rows[0][7].ToString());
                        trustful = (nTrustful == 0) ? "较低" : "较高";
                        trustfulComments = (nTrustful == 0) ? "他（她）答题时前后心理状态略不稳定，疲劳、环境干扰、心绪波动等均可能导致这种偏误" : "他（她）答题时心理状态基本稳定，此方面导致的测评偏误较小";
                        if (trustful.Equals("较高"))
                        {
                            b3 = true;
                        }
                        //作答一致性等级，判定<<<<<<<<<<<<<<<<<<<

                        //作答时间，判定>>>>>>>>>>>>>>>>>>
                        TimeSpan ts = DateTime.Parse(testerInfoTable.Rows[0][3].ToString()) -
                            DateTime.Parse(testerInfoTable.Rows[0][2].ToString());

                        double nRealTime = (ts.TotalSeconds) / (double)60;

                        string answerTimeComments = string.Empty;
                        if (nRealTime <= refMinMinutes)
                        {
                            answerTimeComments = "答题速度过快，有可能是答题过程不够认真或是其他原因，有待进一步了解";
                        }
                        else if (nRealTime >= refMaxMinutes)
                        {
                            answerTimeComments = "答题速度偏慢，背后原因有待进一步了解";
                        }
                        else if (nRealTime > refMinMinutes && nRealTime < refMaxMinutes)
                        {
                            b1 = true;
                            answerTimeComments = "答题速度比较适中";
                        }
                        else
                        {
                            answerTimeComments = string.Empty;
                        }
                        //作答时间，判定<<<<<<<<<<<<<<<<<<<<<                       


                        //提取智力维度，显示柱状图
                        rptDataSet ds = new rptDataSet();
                        for (int j = 0; j < dimTable.Rows.Count; j++)
                        {
                            if (dimTable.Rows[j][5].ToString().Trim().Equals("3"))//智力潜能类
                            {
                                ds.ZhiliDimScore.AddZhiliDimScoreRow(
                                    dimTable.Rows[j][0].ToString().Trim(),
                                    dimTable.Rows[j][1].ToString().Trim(),
                                    dimTable.Rows[j][2].ToString().Trim());
                            }
                        }

                        //删除智力维度在维度得分表里。
                        for (int k = 0; k < dimTable.Rows.Count; k++)
                        {
                            if (dimTable.Rows[k][5].ToString().Trim().Equals("3"))//智力潜能类
                            {
                                dimTable.Rows[k].Delete();
                                dimTable.AcceptChanges();
                                k--;
                            }
                        }

                        //删除三个特殊维度在维度得分表里。
                        for (int s = 0; s < dimTable.Rows.Count; s++)
                        {
                            if (dimTable.Rows[s][1].ToString().Trim().Equals("社会称许性") ||
                                dimTable.Rows[s][1].ToString().Trim().Equals("心理健康度") ||
                                dimTable.Rows[s][1].ToString().Trim().Equals("诚信"))
                            {
                                dimTable.Rows[s].Delete();
                                dimTable.AcceptChanges();
                                s--;
                            }
                        }

                        string xljk = string.Empty;
                        string xljkLevel = string.Empty;
                        string xljkComnet = string.Empty;

                        xljk = rptBLL.GetXljkLevelAndComments(strGUID);//获取心理健康等级和评语
                        if (!string.IsNullOrEmpty(xljk))
                        {
                            int n = xljk.IndexOf("-");
                            xljkLevel = xljk.Substring(0, n);
                            xljkComnet = xljk.Substring(n + 1, xljk.Length - n - 1);
                        }
                        //数据准备完毕！！！



                        //向报表数据源中插入数据。
                        ds.Report_ONE.AddReport_ONERow("社会称许性等级", shcxxDENGJI, "社会称许性评定", shcxxPINGDING);
                        ds.Report_ONE.AddReport_ONERow("作答一致性等级", trustful, "测评工具信度", "0.857　～　0.949");
                        ds.Report_ONE.AddReport_ONERow("作答实际时间", ((int)ts.TotalMinutes).ToString()+"分钟", "答题时间参照值", strREFtime + "分钟");
                        ds.Report_ONE.AddReport_ONERow("答题诚信表现", cxbxPY, "心理健康度状况", xljkLevel);

                        ds.Report_ONE2.AddReport_ONE2Row("社会称许性的相关描述：", "　　1、" + shcxxMIAOSHU + "；");
                        ds.Report_ONE2.AddReport_ONE2Row("做答一致性的相关描述：", "　　2、" + trustfulComments + "；");
                        ds.Report_ONE2.AddReport_ONE2Row("作答时间的相关描述：", "　　3、" + answerTimeComments + "；");
                        ds.Report_ONE2.AddReport_ONE2Row("心理健康的相关描述：", "　　4、" + xljkComnet + "。");

                        ds.zhiweilieshi.AddzhiweilieshiRow(lieshi);
                        ds.zhiweiyoushi.AddzhiweiyoushiRow(youshi);
                        ds.Guanli.AddGuanliRow(sbYRJY.ToString());
                        ds.Qita.AddQitaRow("其他维度名称");
                        ds.gerenfazhan.AddgerenfazhanRow(sbGRFZ.ToString());

                        ReportViewer1.LocalReport.ReportPath = MapPath("Report.rdlc");
                        ReportDataSource rds0 = new ReportDataSource("DimensionScore", dimTable);//注意这里的name和报表中的一致
                        ReportDataSource rds1 = new ReportDataSource("TesterInfo", testerInfoTable);
                        ReportDataSource rds2 = new ReportDataSource("Report_ONE", ds.Tables[2]);
                        ReportDataSource rds3 = new ReportDataSource("ZhiliDimScore", ds.Tables[3]);
                        ReportDataSource rds4 = new ReportDataSource("Report_ONE2", ds.Tables[4]);
                        ReportDataSource rds5 = new ReportDataSource("zhiweiyoushi", ds.Tables[5]);
                        ReportDataSource rds6 = new ReportDataSource("zhiweilieshi", ds.Tables[6]);
                        ReportDataSource rds7 = new ReportDataSource("Guanli", ds.Tables[7]);
                        ReportDataSource rds8 = new ReportDataSource("Qita", ds.Tables[8]);
                        ReportDataSource rds9 = new ReportDataSource("gerenfazhan", ds.Tables[9]);

                        ReportParameter rp5Hidden = new ReportParameter("p5Visible", (!p5Visible).ToString());
                        ReportParameter rp6Hidden = new ReportParameter("p6Visible", (!p6Visible).ToString());

                        bool b = false;
                        if (b1 == true && b2 == true && b3 == true)
                        {
                            b = true;
                        }
                        ReportParameter rptBelievable = new ReportParameter("rptBelievable", b.ToString());


                        ReportViewer1.LocalReport.DataSources.Add(rds0);
                        ReportViewer1.LocalReport.DataSources.Add(rds1);
                        ReportViewer1.LocalReport.DataSources.Add(rds2);
                        ReportViewer1.LocalReport.DataSources.Add(rds3);
                        ReportViewer1.LocalReport.DataSources.Add(rds4);
                        ReportViewer1.LocalReport.DataSources.Add(rds5);
                        ReportViewer1.LocalReport.DataSources.Add(rds6);
                        ReportViewer1.LocalReport.DataSources.Add(rds7);
                        ReportViewer1.LocalReport.DataSources.Add(rds8);
                        ReportViewer1.LocalReport.DataSources.Add(rds9);

                        ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp5Hidden, rp6Hidden });
                        ReportViewer1.LocalReport.Refresh();
                    }
                }
            }
        }
    }
}